package com.fotkidemo.network.json;

import com.fotkidemo.beans.EntryElement;
import com.fotkidemo.beans.Image;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: roma
 * Date: 6/21/12
 * Time: 7:41 PM
 * To change this template use File | Settings | File Templates.
 */
public class JsonImagesParser extends AbstractJsonEntryParser {
    private static final String TAG = "JsonImagesParser";

    public JsonImagesParser(String json) {
        super(json);
    }

    @Override
    protected EntryElement parseEntry(JSONObject entry, String id, String author, String title, Date date) throws JSONException {
        Image image = new Image(id, author, title, date);
        JSONObject img = entry.getJSONObject("img");

        JSONObject xxs = img.getJSONObject("XXS");
        int widthXXS = xxs.getInt("width");
        image.setWidthXXS(widthXXS);
        String hrefXXS = xxs.getString("href");
        image.setHrefXXS(hrefXXS);
        int heightXXS = xxs.getInt("height");
        image.setHeightXXS(heightXXS);

        JSONObject l = img.getJSONObject("L");
        int widthL = l.getInt("width");
        image.setWidthL(widthL);
        String hrefL = l.getString("href");
        image.setHrefL(hrefL);
        int heightL = l.getInt("height");
        image.setHeightL(heightL);

        JSONObject m = img.getJSONObject("M");
        int widthM = m.getInt("width");
        image.setWidthM(widthM);
        String hrefM = m.getString("href");
        image.setHrefM(hrefM);
        int heightM = m.getInt("height");
        image.setHeightM(heightM);

        return image;
    }

    @Override
    protected String getTag() {
        return TAG;
    }
}
